#pragma once

#include <iostream>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"

using namespace std;


Log log;
const std::string dictname = "./dict.txt";
const std::string sep = ":";

static bool Split(string &s, string& part1, string& part2)
{
    auto pos = s.find(sep);
    if(pos == string::npos)
        return false;
    part1 = s.substr(0, pos);
    part2 = s.substr(pos + 1);
    return true;
}

class Init
{
public:
    Init()
    {
        ifstream in(dictname);
        if(!in.is_open())
        {
            log(Fatal, "ifstream open %s error", dictname.c_str());
            exit(6);
        }

        std::string line;
        while(std::getline(in, line))
        {
            std::string part1, part2;
            Split(line, part1, part2);
            dict.insert({part1, part2});
        }
        in.close();
    }

    string translation(const string &key)
    {
        auto pos = dict.find(key);
        if(pos == dict.end())
            return "Unkonw";
        else
            return pos->second;
    }

private:
    unordered_map<string, string> dict;
};